package day7.homework;

import com.sun.tracing.dtrace.StabilityLevel;

import java.util.*;
//实现斗地主随机发牌的功能 一共54张牌
//牌的花色分为   梅花   红桃   黑桃  方片
//牌有  3  4 5 6 7 8 9 10 J  Q K  A  2  大王 小王
//随机发给两个农民 和一个地主      地主 多三张牌
//最后显示每人的牌的结果  并且每人的排要根据牌的大小进行降序排列
public class DouDiZhu {
    public static void main(String[] args) {
        List<Card> cards = new ArrayList();
        String[] color = {"♣","♠","♥","♦"};
        String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        for (String s : color) {
            for (int i = 0; i < number.length; i++) {
                cards.add(new Card(s+number[i],i+3));
            }
        }
        String s;
        cards.add(new Card("小🃏",16));
        cards.add(new Card("大🃏",17));
        Collections.shuffle(cards);
        Set<Card> play1 = new TreeSet<>((c1,c2)->{
           int i = c2.num-c1.num;
           if (i==0) i = c1.str.compareTo(c2.str);
           return i;
        });
        Set<Card> play2 = new TreeSet<>((c1,c2)->{
            int i = c2.num-c1.num;
            if (i==0) i = c1.str.compareTo(c2.str);
            return i;
        });
        Set<Card> play3 = new TreeSet<>((c1,c2)->{
            int i = c2.num-c1.num;
            if (i==0) i = c1.str.compareTo(c2.str);
            return i;
        });
        Random r = new Random();
        while (play1.size()<17&&play2.size()<17&&play3.size()<17){
            for (int i = 0; i < 3; i++) {
                if (i%3==0) {
                    play1.add(cards.get(0));
                    cards.remove(0);
                }else if (i%3==1){
                    play2.add(cards.get(0));
                    cards.remove(0);
                }else{
                    play3.add(cards.get(0));
                    cards.remove(0);
                }
            }

        }
        int landlordIndex = r.nextInt(3);
        boolean landlord1 = false;
        boolean landlord2 = false;
        if (landlordIndex%3==0) {
            play1.addAll(cards);
            landlord1 = true;
        }else if (landlordIndex%3==1) {
            play2.addAll(cards);
            landlord2 = true;
        }else play3.addAll(cards);
        System.out.println("底牌为:"+cards);
        if (landlord1 == true){
            System.out.println("地主："+play1);
            System.out.println("农民1:"+play2);
            System.out.println("农民2:"+play3);
        }else if (landlord2 == true){
            System.out.println("农民1:"+play1);
            System.out.println("地主:"+play2);
            System.out.println("农民2:"+play3);
        }else {
            System.out.println("农民1:"+play1);
            System.out.println("农民2:"+play2);
            System.out.println("地主:"+play3);
        }
    }
}
class Card{
    String str;//存储内容 ♣10
    int num;//存储牌对应的数值
    public Card(String str, int num) {
        this.str = str;
        this.num = num;
    }
    public String toString() {
        return str;
    }
}